package com.ctsi.security.service;

import com.ctsi.framework.mybatis.support.MybatisGenericDao;
import com.ctsi.security.domain.DataObjectType;
import com.ctsi.security.domain.RoleDataObject;
import com.ctsi.security.domain.UserDataObject;
import com.ctsi.security.manager.impl.SecurityImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class SecurityImplDao {
    @Autowired
    private MybatisGenericDao dao;

    /**
     * 取一个角色所具的所有功能
     * 必须排序
     *
     * @param roleId
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<String> getPermissionIds(String roleId) {
        return (List<String>) dao.queryForList(SecurityImpl.class.getName() + ".getPermIdsByRoleId", roleId);
    }

    public Integer getRoleDoIdData(Map<String, Object> map) {
        return (Integer) dao.queryForObject(SecurityImpl.class.getName() + ".selDataObjectId", map);
    }

    @SuppressWarnings("unchecked")
    public List<DataObjectType> getdoType(String doTypeId) {
        return (List<DataObjectType>) dao.queryForList(SecurityImpl.class.getName() + ".queryType", doTypeId);
    }

    @SuppressWarnings("unchecked")
    public List<RoleDataObject> getRoleDataObjects(String roleId, String doTypeId) {
        return (List<RoleDataObject>) dao.queryForList(SecurityImpl.class.getName() + ".getRoleDataObjects", roleId);
    }

    /**
     * 取得角色在某一数据对象类型上对于操作Action的数据对象的列表
     *
     * @param roleId
     * @param doTypeId
     * @param action
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<String> getRoleDataObjectIds(String roleId, String doTypeId, String action) {
        Map<String, String> args = new HashMap<String, String>();
        args.put("roleId", roleId);
        args.put("doTypeId", doTypeId);
        args.put("action", action);
        return (List<String>) dao.queryForList(SecurityImpl.class.getName() + ".getRoleDataObjectIds", args);
    }

    @SuppressWarnings("unchecked")
    public Map<String, Object> getDataObjectBytype(Map<String, Object> params) {
        return dao.queryForMap(SecurityImpl.class.getName() + ".queryDataObjectBytype", params, "dataObjectId");

    }

    @SuppressWarnings("unchecked")
    public List<UserDataObject> initUserResIds(String userLoginId) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(UserDataObject.class.getName());
        stringBuilder.append(".initUserResIds");
        return (List<UserDataObject>) dao.queryForList(stringBuilder.toString(), userLoginId);
    }

    @SuppressWarnings("unchecked")
    public List<String> getUserResObject(String userLoginId, String resType, String action) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", userLoginId);
        params.put("resTypeId", resType);
        params.put("action", action);

        StringBuilder stringBuilder = new StringBuilder(SecurityImpl.class.getName());

        stringBuilder.append(".queryUserResIds");
        return (List<String>) dao.queryForList(stringBuilder.toString(), userLoginId);
    }


}
